package controleur;

import gui.SudokuFrame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import sudoku.SudokuGridMaker;

public class SudokuController implements IObserver
{
	SudokuFrame frame;
	SudokuGridMaker model;
    
	public SudokuController() throws IOException
	{
		model = new SudokuGridMaker();
    	model.addObserver(this);
	}

	public void setSudokuFrame(SudokuFrame sudokuFrame)
	{
		this.frame = sudokuFrame;
	}
	
	@Override
	public void update(IObservable o, Object arg) {
		// TODO Auto-generated method stub
		if (((String)arg).equalsIgnoreCase("Solved"))
		{
			// Display the grid.
			frame.updateGridPanel(model.getSudokuGrid());
			frame.repaint();
	    	frame.setVisible(true);
	    	
			long elapsedTime = model.getElapsedTime();
			String message = "The grid is solved.\nExecution time = " + (double)elapsedTime / 1000000000.0 + " seconds.";
			JOptionPane.showMessageDialog(frame, message,"Sudoku", JOptionPane.INFORMATION_MESSAGE);
		}
		else if (((String)arg).equalsIgnoreCase("Unsolved"))
		{
			// Display the grid.
			frame.updateGridPanel(model.getSudokuGrid());
			frame.repaint();
	    	frame.setVisible(true);
	    	
			long elapsedTime = model.getElapsedTime();
			String message = "The grid is not solvable.\nExecution time = " + (double)elapsedTime / 1000000000.0 + " seconds.";
			JOptionPane.showMessageDialog(frame, message,"Sudoku", JOptionPane.ERROR_MESSAGE);
		}
		else if (((String)arg).equalsIgnoreCase("Unsolved: grid is not valid"))
		{
			// Display the grid.
			frame.updateGridPanel(model.getSudokuGrid());
			frame.repaint();
	    	frame.setVisible(true);
	    	
			long elapsedTime = model.getElapsedTime();
			String message = "The grid is not valid.\nExecution time = " + (double)elapsedTime / 1000000000.0 + " seconds.";
			JOptionPane.showMessageDialog(frame, message,"Sudoku", JOptionPane.ERROR_MESSAGE);
		}
		else
		{
			// Update and repaint the grid.
			//frame.setGrid(model.getSudokuGrid(), true);
			//frame.updateGridPanel(model.getSudokuGrid());
		}
	}
	
    /**
     * @param args
     * @throws Exception 
     */
    public static void main(String [] args) throws Exception
    {
    	SudokuController controller = new SudokuController();
    	SudokuFrame frame = new SudokuFrame();
    	frame.setMenuActionListener(controller.getMenuActionListener());
    	controller.setSudokuFrame(frame);
        frame.setVisible(true);
    	
    	
		/*int [][] sgrid = model.makeSudokuGrid();
		
		for (int i = 0; i < 9; i++)
		{
			for (int j = 0; j < 9; j++)
			{
				System.out.print(sgrid[i][j]);
			}
			System.out.println();
		}*/
		
		/*System.out.println("Grille est valide = " + model.isGridValid());
		System.out.println("Solving grid...");*/
		
		/*if (!model.isGridValid())
			model.notifyObservers(model, "Unsolved: grid is not valid");
		else
			model.solveGrid(0);*/
        
        /*if (showGui) {
            
            frame = new SudokuFrame(grid );
            frame.setVisible(true);
        }*/
    }
    
    public MenuActionListener getMenuActionListener()
    {
    	return new MenuActionListener();
    }
    
    public class MenuActionListener implements ActionListener
	{
    	@Override
    	public void actionPerformed(ActionEvent e)
    	{
		  	if (SudokuFrame.MENU_ACTION_GRID_LOAD.equals(e.getActionCommand()))
		       loadGrid();
		  	else if (SudokuFrame.MENU_ACTION_FILE_QUIT.equals(e.getActionCommand()))
		       frame.quit();
		    else if (SudokuFrame.MENU_ACTION_GRID_SOLVE.equals(e.getActionCommand()))
		    {
				try
				{
					solveGrid();
				}
				catch (InterruptedException e1)
				{
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
		    }
			else if (SudokuFrame.MENU_ACTION_HELP_ABOUT.equals(e.getActionCommand()))
		       frame.showAbout();
    	}
	}
    
    /**
     * Load a new grid.
     */
    public void loadGrid()
    {
    	//System.out.println("Load grid");
    	
    	JFileChooser fc = new JFileChooser();
    	
    	if (fc.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION)
    	{
    		//System.out.println(fc.getSelectedFile().getAbsolutePath());
    		this.model.setSudokuFile(fc.getSelectedFile().getAbsolutePath());
    		this.model.makeSudokuGrid();
    		
        	frame.setGrid(model.getSudokuGrid());
    	}
    }
    
    /**
     * Solve the grid loaded.
     * @throws InterruptedException
     */
    public void solveGrid() throws InterruptedException
    {
    	model.startTimer();
    	if (!model.isGridValid())
    	{
    		model.stopTimer();
    		model.notifyObservers(model, "Unsolved: grid is not valid");
    	}
    	else
    	{
    		model.solveGrid(0);
    	}
    }
}  
    